package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdLogMapper;
import com.heima.admin.service.AdLogService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.AdUserLogDTO;
import com.heima.model.admin.pojos.AdUserLog;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class AdLogServiceImpl extends ServiceImpl<AdLogMapper, AdUserLog> implements AdLogService {
    @Override
    public ResponseResult findByRoAndRm(AdUserLogDTO dto) {
        // 1. 参数校验  (dto 是否为空   分页参数是否正确)
        if(dto == null){
            log.error("参数不能为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam(); // 检查分页
        // 2. 封装查询条件
        // 2.1  分页条件
        Page pageReq = new Page<>(dto.getPage(), dto.getSize());
        // 2.2  查询条件
//        LambdaQueryWrapper<AdUserLog> wrapper = Wrappers.lambdaQuery();
        LambdaQueryWrapper<AdUserLog> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){ //操作方式
            //Non-static method cannot be referenced from a static context
            wrapper.like(AdUserLog::getRequestOperation, dto.getName());
        }
        if(StringUtils.isNotBlank(dto.getStatus())){ //请求方式
            wrapper.like(AdUserLog::getRequestMethod, dto.getStatus());
        }
        if (dto.getBeginDate() != null){
            wrapper.gt(AdUserLog::getCreateTime,dto.getBeginDate());
        }
        if (dto.getEndDate() != null){
            wrapper.lt(AdUserLog::getCreateTime,dto.getEndDate());
        }
       /* if(dto.getCreatedTime()!=null){ // 时间
            String currentDateTime = DateUtils.getCurrentDateTime(dto.getCreatedTime().toString());
            LambdaQueryWrapper<AdUserLog> like = wrapper.like(AdUserLog::getCreatedTime, currentDateTime);
        }*/
        wrapper.orderByDesc(AdUserLog::getCreateTime); // 按照时间降序
        // 3. 封装返回结果
        IPage result = this.page(pageReq, wrapper);
        log.error(result.toString());
        return new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal(),result.getRecords());
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if(id==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        this.removeById(id);
        return ResponseResult.okResult("删除成功");
    }

    /*@Override
    public ResponseResult delete(Integer id) {
        if(id==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        this.removeById(id);
        return ResponseResult.okResult("删除成功");
    }*/
}
